/**
 * 
 */
package com.alstudio.module.voip;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;

import com.alstudio.app.ALLocalEnv;
import com.alstudio.utils.android.media.ALMediaManager;
import com.alstudio.utils.notification.SystemNotificationEnvConfig;
import com.loovee.imaohu.R;

/**
 * @author alonso lee
 * 
 */
public class ALVoipMediaManager {

	// 缺省来电铃声
	private final int DEFAULT_INCOMMING_CALL_RINGTONE = R.raw.msg;
	// 缺省对方挂断铃声
	private final int DEFAULT_HAND_UP_RINGTONE = R.raw.msg;
	// 缺省对方忙铃声
	private final int DEFAULT_BUSY_RINGTONE = R.raw.msg;
	// 缺省回铃音
	private final int DEFAULT_RING_BACK_TONE = R.raw.msg;

	private ALMediaManager mALMediaManager;

	/**
	 * 
	 */
	public ALVoipMediaManager(Context ctx) {
		// TODO Auto-generated constructor stub
	}

	public void setALMediaManager(ALMediaManager mALMediaManager) {
		this.mALMediaManager = mALMediaManager;
	}

	public synchronized void playRing(int res) {

		mALMediaManager.playRingAndVibrate(SystemNotificationEnvConfig
				.getInstance().getCurSystemNoticeState(), res, true);
	}

	/**
	 * 切换喇叭和听筒
	 * 
	 * @param isOn
	 *            true 设置为喇叭模式 false 为听筒
	 */
	public void setSpeakerState(boolean isOn) {
		mALMediaManager.setSpeakerState(isOn);
	}
	
	/**
	 * 检测喇叭是否开启
	 * 
	 * @return
	 */
	public boolean isSpeakerOn() {
		return mALMediaManager.isSpeakerOn();
	}

	/**
	 * 重置为听筒,仅供voip通话使用
	 */
	public void resetSpeaker() {
		mALMediaManager.resetSpeaker();
	}

	/**
	 * 设置通话音量为最大
	 */
	public void setMaxVoiceVolume() {
		mALMediaManager.setMaxVoiceVolume();
	}
	
	public void setMute(boolean isMute){
//		mALMediaManager.setMute(isMute);
		if(isMute){
			Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);
			buttonUp.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEYCODE_HEADSETHOOK));
			ALLocalEnv.getLocalEnv().sendOrderedBroadcast(buttonUp,"android.permission.CALL_PRIVILEGED");
		}
	}

	public boolean isMuted(){
		return mALMediaManager.isMuted();
	}

	/**
	 * 播放指定uri的铃声
	 * 
	 * @param url
	 */
	public synchronized void playRing(String url) {
		Uri uri = Uri.parse(url);
		mALMediaManager.playRingAndVibrate(SystemNotificationEnvConfig
				.getInstance().getCurSystemNoticeState(), uri, false);
	}

	/**
	 * 播放缺省来电铃声
	 */
	public void playIncommingCallRingtone() {
		playRing(DEFAULT_INCOMMING_CALL_RINGTONE);
	}

	/**
	 * 播放指定来电铃声
	 * 
	 * @param res
	 */
	public void playIncommingCallRingtone(int res) {
		playRing(res);
	}

	/**
	 * 播放缺省挂断铃声
	 */
	public void playHandupRingtone() {
		playRing(DEFAULT_HAND_UP_RINGTONE);
	}

	/**
	 * 播放指定挂断铃声
	 * 
	 * @param res
	 */
	public void playHandupRingtone(int res) {
		playRing(res);
	}

	/**
	 * 播放缺省正忙的铃音
	 */
	public void playBusyRingtone() {
		playRing(DEFAULT_BUSY_RINGTONE);
	}

	/**
	 * 播放指定正忙的铃音
	 * 
	 * @param res
	 */
	public void playBusyRingtone(int res) {
		playRing(res);
	}

	/**
	 * 播放缺省回铃音
	 */
	public void playRingBackTone() {
		playRing(DEFAULT_RING_BACK_TONE);
	}

	/**
	 * 播放指定回铃音
	 * 
	 * @param url
	 */
	public void playRingBackTone(String url) {
		playRing(url);
	}

	public void stopVoipRingtone() {
		mALMediaManager.stopPlay();
	}

}
